#include "mainscene.h"
#include "ui_mainscene.h"
#include <QSoundEffect>
#include <QMediaPlayer>
#include <QDebug>

MainScene::MainScene(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);

/***********初始化场景**************/

    this->setWindowIcon(QIcon(Icon_W));

     this->music=new QSound(Bac_Musi,this);
     this->music->setLoops(-1);
     this->music->play();

     this->shotsou=new QSound(Shot_Sou,this);
     this->hadshot=new QSound(Had_Shot,this);

    this->setFixedSize(Wide,Height);

    this->baclab=new QLabel(this);

    this->pix.load(Sce_Path);

    this->baclab->resize(this->size());

    this->baclab->setPixmap(this->pix);

    this->baclab->setScaledContents(true);

    this->sceneinit();

    this->baclab->setMouseTracking(true);
    this->setMouseTracking(true);

    this->shotTim=new QTimer(this);


    /**********信号槽*******************/

   connect(this->startgame,&QPushButton::clicked,this,&MainScene::dealstart);
   connect(this->exitbut,&QPushButton::clicked,this,&MainScene::dealexit);
   connect(this->shotTim,&QTimer::timeout,this,&MainScene::dealshot);
   connect(this->restart,&QPushButton::clicked,this,&MainScene::dealrestart);

}

void MainScene::sceneinit()
{
  /*********开始按钮************/
     this->startflag=false;
     this->startgame=new Button(St_Bu);
     this->startgame->setParent(this);
     this->startgame->resize(200,200);
     this->startgame->setIconSize(this->startgame->size());
     this->startgame->move(this->width()/2-this->startgame->width()/2,this->height()/2-this->startgame->height()/2);
     this->startgame->setrct();


/***********退出按钮****************/

    this->exitbut=new Button(Ex_Butt);
    this->exitbut->setParent(this);
    this->exitbut->move(this->width()-this->exitbut->width(),0);
    this->exitbut->hide();

 /*******重新开始*************/

      this->restart=new Button(Re_Start);
      this->restart->setParent(this);
      this->restart->setsize(50,50);
      this->restart->move(this->exitbut->x()-this->restart->width(),25);

      this->restart->hide();
      this->restart->setrct();

/*************玩具容器*****************/

    this->toycon=new Labels(this);
    this->toycon->setpos(this);

/***********玩具初始化*******************/

    this->toysinit();

    this->loopinit();


}

void MainScene::paintEvent(QPaintEvent *event)
{







}

void MainScene::mousePressEvent(QMouseEvent *event)
{




//     this->shotTim->stop();

     for(int i=0;i<Loop_Num;i++){

         if(!this->loop[i].isshot){
            this->shotsou->play();
            this->loop[i].isshot=true;
            this->loop[i].pos=this->shotloop.pos;
            this->loop[i].updata();
            this->shotTim->start(Loop_Inv);
            break;

         }


     }








}

void MainScene::mouseMoveEvent(QMouseEvent *event)
{

     if(!this->startflag){

         return QWidget::mouseMoveEvent(event);

     }



       QPoint point=event->globalPos()-this->pos();
       point.setX(point.x()-Loop_W/2);
       point.setY(point.y()-Loop_H/2);
       this->shotloop.pos=point;



      if(point.y()<Mov_H){


//          this->shotloop.pllop.show();
//          this->shotloop.pllop.move(this->shotloop.pos);
//          this->shotloop.updata();
//            this->shotloop.pllop.hide();
            return;

      }



       this->shotloop.pllop.show();
       this->shotloop.pllop.move(point);
       this->shotloop.updata();






}

void MainScene::dealstart()
{
     this->startflag=true;
     this->restart->show();
     this->startgame->hide();
     this->exitbut->show();

     this->toyload();
     this->loopload();

    for(int i=0;i<Loop_Num;i++){

        this->loop[i].isshot=false;
        this->loop[i].iscrash=false;
        this->loop[i].pos=QPoint(Wide,Height);
        this->loop[i].pllop.hide();
      if(i<Toy_Num){
          this->toy[i].isloop=false;

      }


    }

      this->toycon->clearsw();




}

void MainScene::dealexit()
{
     this->startflag=false;
     this->startgame->show();
     this->restart->hide();
     this->exitbut->hide();
     this->toyhide();
     this->loopshow.hide();
     this->shotloop.pllop.hide();
     this->dealrestart();

}

void MainScene::dealshot()
{
    this->shotTim->stop();




    for(int i=0;i<Loop_Num;i++){

        if(loop[i].isshot&&!this->loop[i].isout){

            this->loop[i].pos.setY(this->loop[i].pos.y()-Loop_Sped);
            this->loop[i].updata();
            this->loop[i].pllop.move(this->loop[i].pos);
            this->loop[i].pllop.show();

            this->loop[i].intersets(this->toy);

            if(loop[i].iscrash&&!this->loop[i].isout){


              this->loop[i].updata();
              this->loop[i].isout=true;
              this->loop[i].pllop.show();



                    }

            else if(loop[i].pos.y()<350&&!this->loop[i].isout){

                   this->loop[i].pos=QPoint(Wide,Height);
                   loop[i].pllop.move(this->loop[i].pos);
                   this->loop[i].isout=true;
                   this->loop[i].pllop.hide();


                    }









        }




    }



    for(int i=0;i<Loop_Num;i++){

        if(!loop[i].isout){

      for(int j=0;j<Toy_Num;j++){

        if(!this->toy[j].isloop){

        this->shotTim->start(Loop_Inv);

            return;

          }


        }

      break;

        }

    }







      for(int i=0;i<Toy_Num;i++){

          if(this->toy[i].isloop){

             this->toycon->loadpix(toy[i].pix);

          }

      }




        this->toycon->showtoy();




     this->shotTim->stop();





}

void MainScene::dealrestart()
{
     for(int i=0;i<Loop_Num;i++){

         this->loop[i].isshot=false;
         this->loop[i].iscrash=false;
         this->loop[i].isout=false;
         this->loop[i].pos=QPoint(Wide,Height);
         this->loop[i].pllop.hide();
         if(i<Toy_Num){

             this->toy[i].isloop=false;

         }


     }

     this->toycon->clearsw();


}

void MainScene::toysinit()
{



    for(int i=0;i<Toy_Num;i++){

    QString toypath=QString(":/Source/toy%1.png").arg(i);
    this->toy[i].toyinit(toypath,this);

    if(i>4){

        this->toy[i].pos=QPoint(100+((i-5)*(Toy_W+80)),400+ToY_H);

    }else{  this->toy[i].pos=QPoint(50+(i*(Toy_W+80)),400);           }

    this->toy[i].updata();
    this->toy[i].isloop=false;
    this->toy[i].pltoy.hide();



  }




}

void MainScene::toyload()
{


    for(int i=0;i<Toy_Num;i++){

    this->toy[i].updata();
    this->toy[i].pltoy.show();
    this->toy[i].pltoy.move(toy[i].pos);


  }





}

void MainScene::toyhide()
{

    for(int i=0;i<Toy_Num;i++){


    this->toy[i].pltoy.hide();


  }

}

void MainScene::loopinit()
{
    for(int i=0;i<Loop_Num;i++){

       this->loop[i].pix.load(Loop_Path);
       this->loop[i].pos=QPoint(Loop_X,Loop_Y);
       this->loop[i].pllop.setParent(this);
       this->loop[i].pllop.resize(Loop_W,Loop_H);
       this->loop[i].pllop.setPixmap(this->loop[i].pix);
       this->loop[i].pllop.setScaledContents(true);
       this->loop[i].pllop.hide();

    }

     this->shotloop.pix.load(Loop_Path);
     this->shotloop.pllop.setParent(this);
     this->shotloop.shotinit();

     this->shotloop.pllop.hide();







}

void MainScene::loopload()
{

    QPixmap pix;
    pix.load(Loopsw);
    this->loopshow.setParent(this);
    this->loopshow.setPixmap(pix);
    this->loopshow.resize(Loop_W,Loop_H);
    this->loopshow.setScaledContents(true);
    this->loopshow.move(this->width()/2-this->loopshow.width()/2,0);
    this->loopshow.show();


}

void MainScene::loophide()
{

    this->loopshow.hide();


}

void MainScene::iscrash()
{





}

MainScene::~MainScene()
{
    delete ui;
}

